package com.study.talk.ui.app.login.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.study.talk.R
import com.study.talk.ui.app.base.BaseFragment
import com.study.talk.ui.app.home.HomeActivity
import com.study.talk.ui.app.login.LoginActivity
import com.study.talk.ui.app.login.LoginViewModel
import com.study.talk.utils.alert.AlertUtil
import com.study.talk.utils.extras.showToast
import com.study.talk.utils.extras.start

class LoadingFragment : BaseFragment() {
    private val loginActivity by lazy { activity as LoginActivity }

    private val parentViewModel by lazy { ViewModelProvider(loginActivity)[LoginViewModel::class] }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_loading, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        parentViewModel.loginResponse.value?.let {
            //初始化登录信息
            app.login(it)
            //查看是否为首次注册登录用户
            if (it.userInfo.avatar.isNullOrEmpty()) {
                AlertUtil.showCustomDialog(loginActivity, "提醒", "个人信息待完善") {
                    findNavController().navigate(R.id.action_loadingFragment_to_setUserInfoFragment)
                }
            } else {
                showToast("登录成功")
                start(HomeActivity::class.java)
                loginActivity.finish()
            }
        }
    }
}